<?php
/**
 * 前台默认首页
 *
 * @copyright Copyright (c) 2008 luohaili
 * @author harry 
 * @subpackage room
 * 
 */
class controller_FrontDefault extends FLEA_Controller_Action
{
	/**
     * current controller
	 */
	var $controller;

	/**
     * current action
	 */
	var $action;
	
	/**
     * current smarty
	 */
	var $smarty;
	
	function __construct()
	{
		//获取控制器的名字
		$this->controller = strtolower(FLEA::getAppInf('FLEA.internal.currentControllerName'));
		$this->action = strtolower(FLEA::getAppInf('FLEA.internal.currentActionName'));
		
		$this->smarty =& $this->_getView();
		$this->smarty->assign(array(
			'ctrl' => $this->controller,
			'act'  => $this->action,
		));
	}
	
	function __FrontIndex()
	{
		echo 'sdf';
		//echo url('Default','List');
		//$this->smarty->assign('aa','asd');
		//$this->smarty->display('default/home.html');
	}
	
	function __FrontLogin()
	{
		$imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');
		if($_POST){
			do {
				if (!$imgcode->check($_POST['imgcode'])) {
	                $msg = _T('ui_l_invalid_imgcode');
	                break;
	            }
	            $imgcode->clear();
				/**
	             * 验证用户名和密码是否正确
	             */
	            $sysusers =& FLEA::getSingleton('table_SysUsers');
	            /* @var $sysusers Model_SysUsers */
	            $user = $sysusers->findByUsername($_POST['username']);
	            if (!$user) {
	                $msg = _T('ui_l_invalid_username');
	                break;
	            }
	
	            if (!$sysusers->checkPassword($_POST['password'],
	                $user[$sysusers->passwordField]))
	            {
	                $msg = _T('ui_l_invalid_password');
	                break;
	            }
	
	            /**
	             * 登录成功，通过 RBAC 保存用户信息和角色
	             */
	            $data = array();
	            $data['USERNAME'] = $user[$sysusers->usernameField];
	            $data['ID'] = $user[$sysusers->primaryKey];
	
	            $rbac =& FLEA::getSingleton('FLEA_Rbac');
	            /* @var $rbac FLEA_Rbac */
	            $rbac->setUser($data, $sysusers->fetchRoles($user));
	
	            // 保存用户选择的语言
	            $_SESSION['LANG'] = $_POST['language'];

	            if(in_array('USER',$_SESSION['CMS']['RBAC_ROLES']))
	            {
	            	// 店主用户
	            	redirect(url('FrontAdminHotel','FrontAdminHotelIndex'));	
	            }elseif (in_array('CONSUMER',$_SESSION['CMS']['RBAC_ROLES']))
	            {
	            	//个人用户
	            	//redirect(url('FrontAdminHotel','FrontAdminHotelIndex'));	
	            }
	            
			}while(false);
			redirect(url('FrontDefault','FrontLogin'));
		}

		$this->smarty->display('default/login.tpl');
	}
	/**
     * 显示验证码
     */
    function __AdminImgCode() {
        $imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');
        /* @var $imgcode FLEA_Helper_ImgCode */
        $imgcode->image();
    }
}